package face.pay.common.enterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.enterprise.service.IprofitBaseService;
import face.pay.dao.ProfitInfoDao;
import face.pay.entity.ProfitInfo;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: zkj
 * @Date: 2019/7/12 13:05
 * @Description:
 */
@Service
public class ProfitBaseServiceImpl implements IprofitBaseService {

    @Autowired
    ProfitInfoDao profitInfoDao;

    @Override
    @Transactional
    public ResultMessage modifyInfo(ProfitInfo profitInfo, UserInfo loginUser) {

        if(profitInfo.getId()==null){
            profitInfo.setCreatedBy(loginUser.getId());
            profitInfo.setAgencyId(loginUser.getId());
            this.profitInfoDao.insert(profitInfo);
        }else {
            profitInfo.setUpdatedBy(loginUser.getId());

            this.profitInfoDao.update(profitInfo);
        }
        return ResultMessage.createSuccess();
    }

    @Override
    public PageData queryPage(UserInfo loginUser, PageParam pageParam) {

        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        List<ProfitInfo> userInfoList = this.profitInfoDao.queryListByAgencyId(loginUser.getId());

        PageInfo<ProfitInfo> pageInfo = new PageInfo <ProfitInfo>(userInfoList);

        PageData<ProfitInfo> pageData = new PageData<ProfitInfo>(pageInfo.getTotal(),userInfoList);

        return pageData;
    }
}
